package org.jaaksi.view.filterbar;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.*;
import butterknife.ButterKnife;
import org.jaaksi.view.filterbar.fragment.BaseNewHouseListFilterFragment;

/**
 * Created by Fcy on 2015/10/31.
 * 重构筛选框的悬浮实现，在header中筛选地方外围套一个framelayout，高度固定，放置一个filterbar（包含筛选内容区域），
 * 当判断framelayout在屏幕中高度值时，移除filterbar，并将它添加到悬浮出的位置（悬浮出写一个wrap的framelayout<br/>
 *
 * <P>
 * BaseNewHouseListFilterFragment和NewHouseListFilterFragment依旧保留，
 * 去除关于两个filterbar的内容（包括回调和各个view）
 * </P>
 * 1.修改NewHouseListAdapter2只保留一种type
 * 2.改NewHouseListActivity集成于BaseListActivity,但要保留onFilterRefresh等重写的方法 3.
 *
 *
 *
 */
public class MainListActivity extends FragmentActivity
        implements BaseFilterListener
{
	private LinearLayout headView;
	// 筛选条，根据滑动位置判断添加到哪里
	private View mFilterBar;
	// header中的筛选框容
	private FrameLayout container_header_filter;

	// // 悬浮的筛选框部分，包括筛选panel
	private View container_filter;
	// // 悬浮的筛选框容器
	private FrameLayout container_filter_bar;

	private FilterBarController mFilterBarController;

	private int LIMIT_OFFSET;
	private int FILTER_BAR_HEIGHT;
	/**
	 * 是否悬浮状态
	 */
	private boolean isStick = false;

	private int[] size = new int[2];
	private BaseNewHouseListFilterFragment filterFragment;
	private ListView listView;

	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initView();
	}

	/**
	 * header上面的部分是从服务器拉去的，掌链需要一开始筛选框就出来，可以先把上面的view默认都gone掉，拉去数据后再显示
	 */
	private void initHeaderView()
	{
		headView = (LinearLayout) getLayoutInflater()
		        .inflate(R.layout.view_header, null);
		container_header_filter = (FrameLayout) headView
		        .findViewById(R.id.container_header_filter);
		mFilterBar = container_header_filter.findViewById(R.id.lyt_list_filter);
		initFilterBarView(mFilterBar);
	}

	// 初始化filterbar的各种文字等 mFilterBarController.updateAreaUI("海淀");
	private void initFilterBarView(View view)
	{
		filterFragment = new BaseNewHouseListFilterFragment();
		mFilterBarController = FilterBarController.getInstance(this, view,
		        getResources().getStringArray(R.array.filter_content),
		        filterFragment);
		// 为什么能访问到
		// mFilterBarController.clickFilter();
		// TODO 筛选panel 是个Fragment,将fragment添加到悬浮的容器中
		getSupportFragmentManager().beginTransaction()
		        .add(R.id.container_filter_panel, filterFragment)
		        .commitAllowingStateLoss();

	}

	private void initFilterView()
	{
		container_filter = findViewById(R.id.container_filter);
		container_filter_bar = (FrameLayout) container_filter
		        .findViewById(R.id.container_filter_bar);
	}

	private void initView()
	{
		initFilterView();
		initHeaderView();

		FILTER_BAR_HEIGHT = getResources()
		        .getDimensionPixelSize(R.dimen.filter_bar_height);
		// 临界点距离屏幕上方高度
		LIMIT_OFFSET = getResources()
		        .getDimensionPixelSize(R.dimen.title_height) + 55; // 55是通知栏高度，要改成获取
		listView = (ListView) findViewById(R.id.list);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
		        android.R.layout.simple_list_item_1,
		        getResources().getStringArray(R.array.arr));
		listView.setAdapter(adapter);

		listView.addHeaderView(headView);

		listView.setOnScrollListener(new AbsListView.OnScrollListener()
		{
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState)
			{

			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
		            int visibleItemCount, int totalItemCount)
			{
				handleFilterView();
			}
		});
	}

	/**
	 * 根据筛选条位置处理是否悬浮
	 */
	private void handleFilterView()
	{
		// int[] size = new int[2];
		Log.i("fcy", "----------------- y=" + size[1]);
		container_header_filter.getLocationInWindow(size);
		if (!isStick)
		{ // 没有悬浮 size[1] < LIMIT_OFFSET && size[1] != 0
			if (size[1] < LIMIT_OFFSET && size[1] != 0)
			{ // 达到悬浮位置，
				Log.i("fcy", "----------------- y=" + size[1]);
				// 移除header中的 filterbar，添加到悬浮框中
				container_header_filter.removeView(mFilterBar);
				container_filter_bar.addView(mFilterBar);
				container_filter.setVisibility(View.VISIBLE);
				isStick = true;
			}
		}
		else
		{ // 悬浮中
			if (size[1] > LIMIT_OFFSET)
			{
				container_filter_bar.removeView(mFilterBar);
				container_header_filter.addView(mFilterBar);
				container_filter.setVisibility(View.GONE);
				isStick = false;
			}
		}
	}

	@Override
	protected void onDestroy()
	{
		if (mFilterBar != null)
		{
			ButterKnife.unbind(mFilterBar);
		}
		ButterKnife.unbind(this);
		super.onDestroy();
	}

	@Override
	public void filter()
	{
		// TODO
	}

	@Override
	public void clickFilterBar(int position)
	{ // 分割线0.7dp可以被忽略
		listView.setSelectionFromTop(1, FILTER_BAR_HEIGHT); // item的高度
	}

	@Override
	public FilterBarController getFilterBarController()
	{
		return mFilterBarController;
	}
}